﻿using System.Linq;
using ExtendedMvc.Tests.Model;
using ExtendedMvc.Tests.Model.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ExtendedMvc.Tests {


	/// <summary>
	///This is a test class for TextAreaTest and is intended
	///to contain all TextAreaTest Unit Tests
	///</summary>
	[TestClass()]
	public class TextAreaTest {

		private static DummyDataSource database;
		private static ModelViewPage<Dummy> view;
		private static ModelViewForm<Dummy> form;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		public static void MyClassInitialize(TestContext testContext) {
			database = new DummyDataSource();
			var dummy = database.Dummies.All().First();
			view = MvcMockHelpers.FakeViewPage("Index", dummy);
			form = new ModelViewForm<Dummy>(view.Html);
		}

		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup() {
		//}
		//
		//Use TestInitialize to run code before running each test
		[TestInitialize()]
		public void MyTestInitialize() {
			view.ViewContext.ViewData.ModelState.Clear();
		}

		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup() {
		//}
		//
		#endregion

		/// <summary>
		///A test for TextArea Constructor
		///</summary>
		[TestMethod()]
		public void TextArea_WithPropertyName() {
			// Arrange
			var name = "BasicProperty";
			var dummy = database.Dummies.All().First();
			var expectedOutput = string.Format(@"<textarea id=""{1}"" name=""{1}"">{0}</textarea>", dummy.BasicProperty, name);

			// Act
			var textArea = form.TextArea(name);
			var output = textArea.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for TextArea Constructor
		///</summary>
		[TestMethod()]
		public void TextArea_ForModel() {
			// Arrange
			var dummy = database.Dummies.All().First();
			var expectedValue = dummy.BasicProperty;
			var expectedName = "BasicProperty";
			var expectedOutput = string.Format(@"<textarea id=""{1}"" name=""{1}"">{0}</textarea>", expectedValue, expectedName);

			// Act
			var textArea = form.TextArea(m => m.BasicProperty);
			var output = textArea.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for TextArea Constructor
		///</summary>
		[TestMethod()]
		public void TextArea_ForRequiredField() {
			// Arrange
			var dummy = database.Dummies.All().First();
			var expectedValue = dummy.RequiredProperty;
			var expectedName = "RequiredProperty";
			var expectedOutput = string.Format(@"<textarea id=""{1}"" name=""{1}"" required=""required"">{0}</textarea>", expectedValue, expectedName);

			// Act
			var textArea = form.TextArea(m => m.RequiredProperty);
			var output = textArea.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for TextArea Constructor
		///</summary>
		[TestMethod()]
		public void TextArea_ForRequiredField_WithValidationError() {
			// Arrange
			view.ViewContext.ViewData.ModelState.AddModelError("RequiredProperty", "Field is required.");
			var dummy = database.Dummies.All().First();
			var expectedValue = dummy.RequiredProperty;
			var expectedName = "RequiredProperty";
			var expectedOutput = string.Format(@"<textarea class=""input-validation-error"" id=""{1}"" name=""{1}"" required=""required"">{0}</textarea>", expectedValue, expectedName);

			// Act
			var textArea = form.TextArea(m => m.RequiredProperty);
			var output = textArea.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for TextArea Constructor
		///</summary>
		[TestMethod()]
		public void TextArea_ForRequiredField_WithValidationMessage() {
			// Arrange
			var errrorMessage = "Field is required.";
			view.ViewContext.ViewData.ModelState.AddModelError("RequiredProperty", errrorMessage);
			var dummy = database.Dummies.All().First();
			var expectedValue = dummy.RequiredProperty;
			var expectedName = "RequiredProperty";
			var expectedOutput = string.Format(@"<textarea class=""input-validation-error"" id=""{1}"" name=""{1}"" required=""required"">{0}</textarea><span class=""field-validation-error"" id=""RequiredProperty_Error"">{2}</span>", expectedValue, expectedName, errrorMessage);

			// Act
			var textArea = form.TextArea(m => m.RequiredProperty).AddValidationMessage();
			var output = textArea.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for TextArea Constructor
		///</summary>
		[TestMethod()]
		public void TextArea_WithMaxLength() {
			// Arrange
			var dummy = database.Dummies.All().First();
			var expectedValue = dummy.SizedProperty;
			var expectedName = "SizedProperty";
			var expectedOutput = string.Format(@"<textarea id=""{1}"" maxlength=""240"" name=""{1}"">{0}</textarea>", expectedValue, expectedName);

			// Act
			var textArea = form.TextArea(m => m.SizedProperty).MaxLength(240);
			var output = textArea.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for TextArea Constructor
		///</summary>
		[TestMethod()]
		public void TextArea_WithRows() {
			// Arrange
			var dummy = database.Dummies.All().First();
			var expectedValue = dummy.SizedProperty;
			var expectedName = "SizedProperty";
			var expectedOutput = string.Format(@"<textarea id=""{1}"" name=""{1}"" rows=""4"">{0}</textarea>", expectedValue, expectedName);

			// Act
			var textArea = form.TextArea(m => m.SizedProperty).Rows(4);
			var output = textArea.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for TextArea Constructor
		///</summary>
		[TestMethod()]
		public void TextArea_WithColumns() {
			// Arrange
			var dummy = database.Dummies.All().First();
			var expectedValue = dummy.SizedProperty;
			var expectedName = "SizedProperty";
			var expectedOutput = string.Format(@"<textarea cols=""90"" id=""{1}"" name=""{1}"">{0}</textarea>", expectedValue, expectedName);

			// Act
			var textArea = form.TextArea(m => m.SizedProperty).Columns(90);
			var output = textArea.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for TextArea Constructor
		///</summary>
		[TestMethod()]
		public void TextArea_WithSize() {
			// Arrange
			var dummy = database.Dummies.All().First();
			var expectedValue = dummy.SizedProperty;
			var expectedName = "SizedProperty";
			var expectedOutput = string.Format(@"<textarea cols=""90"" id=""{1}"" name=""{1}"" rows=""4"">{0}</textarea>", expectedValue, expectedName);

			// Act
			var textArea = form.TextArea(m => m.SizedProperty).Size(90, 4);
			var output = textArea.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}
	}
}
